<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ContractController extends Controller
{
	public function getIndex(Request $request)
	{
		if($request['name']){
			$res = DB::table('contract')
			->where('Name','like','%'.$request['name'].'%')
            ->paginate(10);
		}else{
			$res = DB::table('contract')
			->paginate(3);
		}
		// dd($res);
		 $info = $request -> all();
		return view('Lzk.Contract.Contract',['res'=>$res,'info'=>$info]);
	}

	//解析添加
	public function getAdd()
	{
		return view('Lzk.Contract.AddContract');
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$ins = $request->except('_token');

		$class = $ins['Class'];
		// dd($class);
		if($class == 0){
			echo '<script>alert("请选择正确合同类型");window.location.href="/Lzk/Contract/add";</script>';
			die;
		}

		$pname = $ins['PersonName'];
		$name = DB::table('person')->where('PersonName',$pname)->first();
		// dd($name);
		if(!$name){
			echo '<script>alert("请填写正确的员工名");window.location.href="/Lzk/Contract/add";</script>';
			die;
		}


		$ins['Date'] = time();
		$ins['State'] = 1;
		// dd($ins);
		$res = DB::table('contract')->insert($ins);
		if($res){
			return redirect('Lzk/Contract');
		}else{
			return back();
		}
	}

	public function getLook($id)
	{
		$res = DB::table('contract')->where('id',$id)->first();
		// dd($res);
		return view('Lzk.Contract.LookContract',['res'=>$res]);
	}

	public function postState(Request $request)
	{
		$name = $request->session()->get('username');
		// dd($name);
		//修改状态
		// dd($request->all());
		//查询数据库
		$id = $request->only('id');
		$res = DB::table('contract')->where('id',$id)->first();
		$res['State'] = 2;
		// dd($res);

		$res1 = DB::table('contract')->where('id',$id)->first();
		$res1['State'] = 1;
		// dd($res1);
	

		$st = $request->only('State');
		$sinfo = $st['State'];
		// dd($st);
		if($sinfo == 2){
			DB::table('contract')->where('id',$id)->update($res);
			return redirect('Lzk/Contract');
		}

		if($sinfo == 1){
			DB::table('contract')->where('id',$id)->update($res1);
			return redirect('Lzk/Contract');
		}
	}

	public function getDel($id)
	{
		$res = DB::table('contract')->where('id',$id)->first();
		// dd($res);
		$sinfo = $res['State'];
		if($sinfo == 2){
			echo '<script>alert("此合同已审批,请勿删除");window.location.href="/Lzk/Contract";</script>';
			die;
		}else{
			DB::table('contract')->where('id',$id)->delete($id);
			return redirect('Lzk/Contract');
		}
	}

	public function getEdit($id)
	{
		//解析添加页面
		$res = DB::table('contract')->where('id',$id)->first();
		$einfo = $res['State'];
		if($einfo == 2){
			echo '<script>alert("此合同已审批,请勿修改");window.location.href="/Lzk/Contract";</script>';
			die;
		}
		return view('Lzk.Contract.EditContract',['res'=>$res]);
	}

	public function postUpdate(Request $request)
	{
		$id = $request->only('id');
		$res = $request->only('Name','Class','Content','Time','PersonName','Company');
		$class = $res['Class'];
		// dd($class);
		if($class == 0){
			echo '<script>alert("请选择正确合同类型");window.location.href="/Lzk/Contract";</script>';
			die;
		}

		$pname = $res['PersonName'];
		$name = DB::table('person')->where('PersonName',$pname)->first();
		// dd($name);
		if(!$name){
			echo '<script>alert("请填写正确的员工名");window.location.href="/Lzk/Contract";</script>';
			die;
		}

		$updat = DB::table('contract')->where('id',$id)->update($res);
		if($updat){
			return redirect('Lzk/Contract');
		}else{
			return back();
		}

	}


}